Shell Programming
Shell programming basics (Detailed)
Section titled “Shell programming basics (Detailed)”-
Shell: Command-line interface between user and OS that interprets and executes commands.
-
Common shells:
sh(Bourne shell),bash(Bourne Again Shell, default in Linux),zsh(Z shell). -
Script file extension:
.sh -
Execution:
Terminal window bash file.sh # run using bashchmod +x file.sh # make executable./file.sh # execute script directly -
Shebang (
#!): Specifies interpreter for the script, e.g.,#!/bin/bash.
Variables
Section titled “Variables”-
User-defined variables: No spaces around
=Terminal window x=10name="SEBI"echo $x$expands variable value. -
Environment variables:
-
HOME: Current user’s home directory. -
PATH: Colon-separated directories where shell searches commands.
-
-
Exporting variables:
Terminal window export xMakes variable available to child processes/subshells.
Input and Output
Section titled “Input and Output”-
Read input:
Terminal window read x -
Prompted input:
Terminal window read -p "Enter value: " x -
Print output:
Terminal window echo "Value is $x"
Command substitution
Section titled “Command substitution”-
Store command output:
Terminal window d=$(date) # modern syntaxfiles=`ls` # legacy syntax -
Arithmetic operations:
Terminal window sum=$((a+b))mul=`expr $a \* $b`
Conditional statements
Section titled “Conditional statements”-
ifsyntax:Terminal window if [ $a -gt $b ]; thenecho "a greater"fi -
Comparison operators (integers):
-eq, -ne, -gt, -lt, -ge, -le -
String operators:
=, !=, -z (empty), -n (non-empty) -
if-elsesyntax:Terminal window if [ $a -eq 10 ]; thenecho "Ten"elseecho "Not ten"fi
Case statement
Section titled “Case statement”case $x in 1) echo "One" ;; 2) echo "Two" ;; *) echo "Other" ;;esac-
case: Starts multi-way selection. -
in: Begins pattern list. -
1): Pattern match. -
*): Default case. -
;;: Ends a block. -
esac: Ends case statement.
-
For loop:
Terminal window for i in 1 2 3; doecho $idone -
While loop:
Terminal window while [ $x -lt 5 ]; dox=$((x+1))done -
Until loop:
Terminal window until [ $x -gt 5 ]; dox=$((x+1))done
Functions
Section titled “Functions”fun() { echo "Hello"}fun- Return status via
$?.
Command-line arguments
Section titled “Command-line arguments”$0 # script name$1 $2 ... # arguments$# # number of arguments$@ # all argumentsFile test operators
Section titled “File test operators”-
-f: file exists -
-d: directory exists -
-r: readable,-w: writable,-x: executable
Example:
if [ -f file.txt ]; then echo "Exists"; fiRedirection
Section titled “Redirection”-
>: overwrite output -
>>: append output -
<: input from file -
2>: redirect errors
ls > out.txt- Send output of one command to another:
ps -ef | grep root-
ps: list processes,-ef: full format -
grep: search text/pattern,root: matches processes owned by root
Important Linux commands (exam-focused)
Section titled “Important Linux commands (exam-focused)”-
File operations:
ls cd pwd mkdir rmdir rm cp mv -
View files:
cat less more head tail -
Text processing:
grep awk sed sort uniq wc cut -
Permissions:
chmod chown -
Process management:
ps top kill -
Disk & memory:
df du free
awk & sed
Section titled “awk & sed”-
awk: extract/format text columns
Terminal window awk '{print $1}' file.txt -
sed: find/replace/modify text
Terminal window sed 's/old/new/' file.txt
Exit status
Section titled “Exit status”0= success, non-zero = failure
echo $?Crontab
Section titled “Crontab”- Schedule jobs:
crontab -e-
-e: edit user’s cron jobs -
Format:
min hour day month week commandExtra Tips for SEBI Grade A (IT)
Section titled “Extra Tips for SEBI Grade A (IT)”-
Focus on practical usage of shell commands, loops, conditions, and file operations.
-
Remember command vs arithmetic substitution:
$()vs$(( )). -
Know pipes, redirection, cron jobs, awk & sed basics.
-
Expect questions on environment variables, file tests, and exit status.
If you want, I can create a super-condensed one-page “last-minute revision sheet” for shell scripting for SEBI IT Grade A. It will fit everything exam-relevant in bare minimum lines. Do you want me to do that?